//
//  KnowWebViewVC.m
//  liangyi
//
//  Created by 张水 on 16/12/29.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "KnowWebViewVC.h"
#import "KnowBottomView.h"
#import "DynamicTrainVC.h"
#import "KnowCommentVC.h"
#import "AppHeader.h"
#import "LoginViewController.h"
@interface KnowWebViewVC ()<UIWebViewDelegate,UMSocialUIDelegate>

@property(nonatomic,strong) UIWebView *webview;

@property(nonatomic,copy)NSString *kid;

@property(nonatomic,copy)NSURL *urlString;

@property(nonatomic,copy)NSString *kwonTitle;

@property(nonatomic,strong) KnowBottomView *BottomView;

@end

@implementation KnowWebViewVC

-(void)viewDidLoad{
    [super  viewDidLoad];
    self.title = @"科普知识";
    self.webview.delegate = self;
    [self initUI];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self LoadLikePersonCount];
}
-(void)setModel:(LeftModel *)model{
    _model = model;
    self.kid = model.id;
    self.kwonTitle = model.title;
}
-(void)initUI{
    
    self.webview = [UIWebView new];
    self.webview.scalesPageToFit = YES;
    self.webview.scrollView.bounces = NO;
    [self.webview setOpaque:NO];
    self.webview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webview];
    WeakSelf(wk)
    [self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.equalTo(wk.view);
        make.bottom.equalTo(wk.view).offset(-40);
        make.left.equalTo(wk.view);
        make.right.equalTo(wk.view);
    }];
    
    self.BottomView = [[KnowBottomView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 40, kScreenWidth, 40)];
    [self.view  insertSubview:self.BottomView aboveSubview:self.view.subviews.lastObject];
    [self.BottomView.ForwardBtn addTarget:self action:@selector(ForwardBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self.BottomView.CommentBtn addTarget:self action:@selector(CommentBtnDidCLick) forControlEvents:UIControlEventTouchUpInside];
    [self.BottomView.AppreciateBtn addTarget:self action:@selector(AppreciateBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.urlString = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@%@/%@",BASEURL,kURL_know,self.kid]];
    NSLog(@"%@",self.urlString);
    [self.webview loadRequest:[[NSURLRequest alloc]initWithURL:self.urlString]];
    
}

-(void)ForwardBtnDidClick{
    
    [UMSocialData defaultData].extConfig.qqData.title = self.kwonTitle;
    [UMSocialData defaultData].extConfig.qqData.url = [NSString stringWithFormat:@"%@",self.urlString];
    [UMSocialData defaultData].extConfig.qqData.shareImage = [UIImage imageNamed:@"logo"];
    
    [UMSocialData defaultData].extConfig.wechatSessionData.title = self.kwonTitle;
    [UMSocialData defaultData].extConfig.wechatSessionData.url = [NSString stringWithFormat:@"%@",self.urlString];
    [UMSocialData defaultData].extConfig.wechatSessionData.shareImage = [UIImage imageNamed:@"logo"];
    
    [UMSocialData defaultData].extConfig.wechatTimelineData.title = self.kwonTitle;
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = [NSString stringWithFormat:@"%@",self.urlString];
    [UMSocialData defaultData].extConfig.wechatTimelineData.shareImage = [UIImage imageNamed:@"logo"];
    
    [UMSocialData defaultData].extConfig.qzoneData.title = self.kwonTitle;
    [UMSocialData defaultData].extConfig.qzoneData.url = [NSString stringWithFormat:@"%@",self.urlString];
    [UMSocialData defaultData].extConfig.qzoneData.shareImage = [UIImage imageNamed:@"logo"];
   
    [UMSocialData defaultData].extConfig.sinaData.snsName = self.kwonTitle;
    [UMSocialData defaultData].extConfig.sinaData.shareText = [NSString stringWithFormat:@"%@%@",self.kwonTitle,[NSString stringWithFormat:@"%@",self.urlString]];
    [UMSocialData defaultData].extConfig.sinaData.shareImage = [UIImage imageNamed:@"logo"];
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"58aa453b6e27a4238d0008ae" shareText:@"我在顶医上看到了非常好的科普知识，你也快来看看吧。" shareImage:[UIImage imageNamed:@"logo"] shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina,UMShareToQQ,UMShareToQzone] delegate:self];
}

-(void)CommentBtnDidCLick{
    NSString *userLogin = [CommonMethod fuckNULL:[UserDefaultsUtlis getValueForKey:@"userid"]];
    NSLog(@"----%@",userLogin);
    if (userLogin.length == 0) {
        LoginViewController *vc = [LoginViewController new];
        UINavigationController *navVc = [[UINavigationController alloc]initWithRootViewController:vc];
        [self presentViewController:navVc animated:YES completion:nil];
        return;
    }
    KnowCommentVC *vc = [KnowCommentVC new];
    vc.knowId = self.kid;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)AppreciateBtnDidClick{
    NSString *userLogin = [CommonMethod fuckNULL:[UserDefaultsUtlis getValueForKey:@"userid"]];
    NSLog(@"----%@",userLogin);
    if (userLogin.length == 0) {
        LoginViewController *vc = [LoginViewController new];
        UINavigationController *navVc = [[UINavigationController alloc]initWithRootViewController:vc];
        [self presentViewController:navVc animated:YES completion:nil];
        return;
    }
    
    
    NSString *url = [BASEURL  stringByAppendingString:KURL_Appreciate];
    NSDictionary *dict = @{@"knowId":self.kid};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString,
                                 @"login":[NSString  AES128Encrypt:[NSString stringWithFormat:@"%@@@%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"],ApplicationDelegate.deviceID]]};
    NSLog(@"%@",dict);
    NSLog(@"%@",parameters);
    [AppNetwork requestWithPath:url parameters:parameters  success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0){
            [CustomProgressHUD showSuccessWithStatus:@"恭喜您点赞成功!!!"];
            [self LoadLikePersonCount];
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error];
    }];
}
-(void)LoadLikePersonCount{
    
    NSString *userLogin = [CommonMethod fuckNULL:[UserDefaultsUtlis getValueForKey:@"userid"]];
    NSLog(@"----%@",userLogin);
    if (userLogin.length == 0) {
        return;
    }
    
    NSString *url = [BASEURL  stringByAppendingString:KURL_AppreciateCount];
    NSDictionary *dict = @{@"knowId":self.kid};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString,
                                 @"login":[NSString  AES128Encrypt:[NSString stringWithFormat:@"%@@@%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"],ApplicationDelegate.deviceID]]};
    NSLog(@"%@",dict);
    [AppNetwork requestWithPath:url parameters:parameters  success:^(id responseObject) {
        
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        NSLog(@"111%@",network.dataObject);
        NSLog(@"333%@",network.code);
        NSLog(@"222%@",network.errorDescription);
        if (code == 0){
            [self.BottomView.AppreciateBtn setTitle:[NSString stringWithFormat:@"%@人点赞", network.dataObject] forState:UIControlStateNormal];
        }else{
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error];
    }];
    
}


@end
